package com.powernode.listen;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.powernode.constant.QueueConstant;
import com.powernode.dto.MessageDto;
import com.powernode.utils.PowerAuthMenuUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class MessageQueueListener {

    @RabbitListener(queues = QueueConstant.WX_MSG_QUEUE)
    public void messageQueueListen(Message message, Channel channel) throws IOException {
        String string = new String(message.getBody());
        Map<String, String> jsonObject = JSON.parseObject(string, Map.class);
        String templateId = jsonObject.get("templateId");
        String accessTokenUrl = jsonObject.get("accessTokenUrl");
        String orderNo = jsonObject.get("orderNo");
        String total = jsonObject.get("actualTotal");
        MessageDto messageDto = new MessageDto();
        messageDto.setTemplateId(templateId);
        messageDto.setUrl("http://weixin.qq.com/download");
        messageDto.setTouser("o2Gjm5uOG8P_xO-KhK9bXtfgsvcw");
        messageDto.setTopcolor("#FF0000");
        Map<String, Map<String, String>> map = new HashMap<>();
        Map<String, String> orderNoMap = MessageDto.setDataValue(orderNo, "#173177");
        map.put("orderNo", orderNoMap);
        Map<String, String> totalMap = MessageDto.setDataValue(total, "#173177");
        map.put("total", totalMap);
        HttpUtil.post(accessTokenUrl, JSON.toJSONString(map));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
